const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
  entry: './src/index.js',
  context:process.cwd(),
  mode:'development',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename:'monitor.js'
  },
  devServer: {
    port: 3000,
    static: {
      directory:path.resolve(__dirname,'dist')
    },
    compress: true,
    setupMiddlewares: (middlewares, devServer) => {
      if (!devServer) {
        throw new Error('webpack-dev-server is not defined');
      }
      middlewares.unshift({
        name: 'success',
        path: '/success',
        middleware: (req, res) => {
          res.send('success!');
        },
      });
      middlewares.unshift({
        name: 'error',
        path: '/error',
        middleware: (req, res) => {
          res.sendStatus(500);
        },
      });
      middlewares.push((req, res) => {
        res.send('Hello World!');
      });
      return middlewares
    }
  },
  plugins: [
    new HtmlWebpackPlugin({//自动打包成
      template: './src/index.html',
      inject:'head'
    })
  ]
}